import java.awt.AWTException;
import java.awt.MouseInfo;
import java.awt.Robot;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;

import javax.swing.JTextField;
//1556 32 1100 688 463 348 472 416 520 457 Difosal2 dbgt8457 4000 4000

public class Init {
	public static void main(String[] args) {
		Init init = new Init();
		File f = new File("C:/login.txt");
		if(f.exists()){
			try {
				InputStream is = new FileInputStream(f);
				String result = init.readConfig(is);
				String[] config = result.split(" ");
				if("".equals(config[0])){
					while(true){
						System.out.println("X:" + MouseInfo.getPointerInfo().getLocation().getX()
								+ " Y:" + MouseInfo.getPointerInfo().getLocation().getY());
						try {
							Thread.sleep(200);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						
					}
				}else{
					init.loga(config);
				}
				
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		
		System.exit(0);
/*
		while (true) {
			System.out.println(MouseInfo.getPointerInfo().getLocation().getX()
					+ " " + MouseInfo.getPointerInfo().getLocation().getY());
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}*/
	}

	public static void typeCharacter(Robot robot, String letter) {
		try {
			boolean upperCase = Character.isUpperCase(letter.charAt(0));
			String variableName = "VK_" + letter.toUpperCase();

			KeyEvent ke = new KeyEvent(new JTextField(), 0, 0, 0, 0, ' ');
			Class clazz = ke.getClass();
			Field field = clazz.getField(variableName);
			int keyCode = field.getInt(ke);

			Thread.sleep(20);

			if (upperCase)
				robot.keyPress(KeyEvent.VK_SHIFT);

			robot.keyPress(keyCode);
			robot.keyRelease(keyCode);

			if (upperCase)
				robot.keyRelease(KeyEvent.VK_SHIFT);
		} catch (Exception e) {
			System.out.println(e);
		}
	}

	private void loga(String[] config) {
		try {
			Robot r = new Robot();
			
			r.mouseMove(Integer.parseInt(config[0]), Integer.parseInt(config[1]));
			r.mousePress(InputEvent.BUTTON1_MASK);
			r.mouseRelease(InputEvent.BUTTON1_MASK);
			Thread.sleep(50);
			r.mousePress(InputEvent.BUTTON1_MASK);
			r.mouseRelease(InputEvent.BUTTON1_MASK);
			Thread.sleep(Integer.parseInt(config[12]));//TEMPO CARREGAMENTO ABERTURA lol
			r.mouseMove(Integer.parseInt(config[2]), Integer.parseInt(config[3]));
			r.mousePress(InputEvent.BUTTON1_MASK);
			r.mouseRelease(InputEvent.BUTTON1_MASK);
			Thread.sleep(Integer.parseInt(config[13]));//TEMPO DO BOTAO JOGAR
			r.mouseMove(Integer.parseInt(config[4]), Integer.parseInt(config[5]));
			r.mousePress(InputEvent.BUTTON1_MASK);
			r.mouseRelease(InputEvent.BUTTON1_MASK);
			Thread.sleep(50);
			r.mousePress(InputEvent.BUTTON1_MASK);
			r.mouseRelease(InputEvent.BUTTON1_MASK);
			for(int j=0;j<config[10].length();j++){  //LOGIN
				typeCharacter(r, String.valueOf(config[10].charAt(j)));
			}
			Thread.sleep(100);
			r.mouseMove(Integer.parseInt(config[6]), Integer.parseInt(config[7]));
			r.mousePress(InputEvent.BUTTON1_MASK);
			r.mouseRelease(InputEvent.BUTTON1_MASK);
			for(int j=0;j<config[11].length();j++){  //SENHA
				typeCharacter(r, String.valueOf(config[11].charAt(j)));
			}
			Thread.sleep(100);
			r.mouseMove(Integer.parseInt(config[8]), Integer.parseInt(config[9]));
			r.mousePress(InputEvent.BUTTON1_MASK);
			r.mouseRelease(InputEvent.BUTTON1_MASK);			
			
		} catch (AWTException e) {
			e.printStackTrace();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	private String readConfig(InputStream fis){
		String result = new String();
		int content;
		try {
			while ((content = fis.read()) != -1) {
				result += (char) content;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}
}
